package controller.register;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import mapper.UserMapper;
import dao.util.UserCreate;

import java.io.IOException;

@WebServlet("/register")
public class register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserCreate userCreate =new UserCreate();
        UserMapper userMapper = userCreate.getMapperClass();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String introduction = req.getParameter("introduction");
        String inputcaptcha = req.getParameter("captcha");
        if (inputcaptcha.equals((String) req.getSession().getAttribute("captcha"))) {
            userMapper.insertUser(username, password, email,introduction);
            userCreate.commitsession();
            userCreate.closesession();
            resp.sendRedirect("login.html");
        } else {
            resp.getWriter().write("验证码错误，请返回重新输入！");
            resp.sendRedirect("register.html");
        }


    }
}
